/*
 * Copyright (c) 2020 Nanjing Xiaoxiongpai Intelligent Technology Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

 #include <stdio.h>
 #include <unistd.h>
 
 #include "cmsis_os2.h"
 #include "iot_gpio.h"
 #include "ohos_init.h"
 
 #define LED_TASK_STACK_SIZE (1024 * 4)
 #define LED_TASK_PRIO 25
 #define LED_GPIO 2
 
 // Define durations for SOS signal (adjust as needed)
 #define DOT_DURATION_MS 200   // Short flash
 #define DASH_DURATION_MS 600  // Long flash
 #define SPACE_DURATION_MS 400 // Pause between dots/dashes
 #define LETTER_SPACE_DURATION_MS 800 // Pause between letters
 #define WORD_SPACE_DURATION_MS 1400   // Pause between words
 
 /**
  * @brief led task output high and low levels to turn on and off LED to signal SOS
  *
  */
 static void LedTask(void)
 {
     // init gpio of LED
     IoTGpioInit(LED_GPIO);
 
     // set GPIO_2 is output mode
     IoTGpioSetDir(LED_GPIO, IOT_GPIO_DIR_OUT);
 
     while (1) {
         // S
         IoTGpioSetOutputVal(LED_GPIO, 1);
         usleep(DOT_DURATION_MS * 1000);
         IoTGpioSetOutputVal(LED_GPIO, 0);
         usleep(SPACE_DURATION_MS * 1000);
 
         IoTGpioSetOutputVal(LED_GPIO, 1);
         usleep(DOT_DURATION_MS * 1000);
         IoTGpioSetOutputVal(LED_GPIO, 0);
         usleep(SPACE_DURATION_MS * 1000);
 
         IoTGpioSetOutputVal(LED_GPIO, 1);
         usleep(DOT_DURATION_MS * 1000);
         IoTGpioSetOutputVal(LED_GPIO, 0);
         usleep(LETTER_SPACE_DURATION_MS * 1000);
 
         // O
         IoTGpioSetOutputVal(LED_GPIO, 1);
         usleep(DASH_DURATION_MS * 1000);
         IoTGpioSetOutputVal(LED_GPIO, 0);
         usleep(SPACE_DURATION_MS * 1000);
 
         IoTGpioSetOutputVal(LED_GPIO, 1);
         usleep(DASH_DURATION_MS * 1000);
         IoTGpioSetOutputVal(LED_GPIO, 0);
         usleep(SPACE_DURATION_MS * 1000);
 
         IoTGpioSetOutputVal(LED_GPIO, 1);
         usleep(DASH_DURATION_MS * 1000);
         IoTGpioSetOutputVal(LED_GPIO, 0);
         usleep(LETTER_SPACE_DURATION_MS * 1000);
 
         // S
         IoTGpioSetOutputVal(LED_GPIO, 1);
         usleep(DOT_DURATION_MS * 1000);
         IoTGpioSetOutputVal(LED_GPIO, 0);
         usleep(SPACE_DURATION_MS * 1000);
 
         IoTGpioSetOutputVal(LED_GPIO, 1);
         usleep(DOT_DURATION_MS * 1000);
         IoTGpioSetOutputVal(LED_GPIO, 0);
         usleep(SPACE_DURATION_MS * 1000);
 
         IoTGpioSetOutputVal(LED_GPIO, 1);
         usleep(DOT_DURATION_MS * 1000);
         IoTGpioSetOutputVal(LED_GPIO, 0);
         usleep(WORD_SPACE_DURATION_MS * 1000); 
     }
 }
 
 /**
  * @brief Main Entry of the Led Example
  *
  */
 static void LedExampleEntry(void)
 {
     osThreadAttr_t attr;
 
     attr.name = "LedTask";
     attr.attr_bits = 0U;
     attr.cb_mem = NULL;
     attr.cb_size = 0U;
     attr.stack_mem = NULL;
     attr.stack_size = LED_TASK_STACK_SIZE;
     attr.priority = LED_TASK_PRIO;
 
     if (osThreadNew((osThreadFunc_t)LedTask, NULL, &attr) == NULL) {
         printf("Failed to create LedTask!\n");
     }
 }
 
 APP_FEATURE_INIT(LedExampleEntry);